﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using TaoPortal.BusinessLogic.DTO;
using TaoPortal.BusinessLogic.Extensions;
using TaoPortal.BusinessLogic.Repository;
using TaoPortal.Common;
using TaoPortal.Common.Utility;
using TaoPortal.Entity;

namespace TaoPortal.Areas.Admin.Models
{
    public class ProductSearcher
    {
        public string PName { get; set; }
        public string PModel { get; set; }
        public string PClass { get; set; }
        public string OtherName { get; set; }
        public string OtherValue { get; set; }
        public DateTime? StartTime { get; set; }
        public DateTime? EndTime { get; set; }
        public string Status { get; set; }
    }

    public class AddProduct
    {
        public string ID { get; set; }
        [Required]
        public string PName { get; set; }
        [Required]
        public string PModel { get; set; }
        [Required]
        public Nullable<decimal> PPrice { get; set; }
        public string TitleImage { get; set; }
        [Required]
        public string ClassID { get; set; }
        public string PNumber { get; set; }
        public string Description { get; set; }
        public string Introduction { get; set; }
        public string Status { get; set; }
        [Required]
        public Nullable<System.DateTime> AddDate { get; set; }
        public Nullable<bool> Allowreply { get; set; }

        public List<ProductPro> ProductPro { get; set; }
        public List<TP_ProdImg> ProdImg { get; set; }
    }

    public class ProductPro
    {
        public string ProID { get; set; }
        public string ProText { get; set; }
        public int ProSort { get; set; }
        public string ProValue { get; set; }
    }

    public class ProductModel : BaseModel
    {
        public ProductSearcher Searcher { get; set; }
        public string ProductClass { get; set; }
        public string ProductProStr { get; set; }
        public string ProductImgStr { get; set; }
        public AddProduct Product { get; set; }

        public ProductModel()
        {
            Searcher = new ProductSearcher();
            ProductClass = "[]";
        }

        public List<TreeData> GetClass(string parentid)
        {
            return GetClassByPID(TaoPortal.Common.SysEnum.SysClassType.ProductType, parentid);
        }

        public void InitData(string id)
        {
            var rp = new ProdRepository();
            var prod = rp.GetProdByID(id);
            if (prod != null)
            {
                Product = new AddProduct();
                Product.ID = prod.TP_ProdInfo.ID;
                Product.PName = prod.TP_ProdInfo.PName;
                Product.PModel = prod.TP_ProdInfo.PModel;
                Product.PPrice = prod.TP_ProdInfo.PPrice;
                Product.TitleImage = prod.TP_ProdInfo.TitleImage;
                Product.ClassID = prod.TP_ProdInfo.ClassID;
                Product.PNumber = prod.TP_ProdInfo.PNumber;
                Product.Description = prod.TP_ProdInfo.Description;
                Product.Introduction = prod.TP_ProdInfo.Introduction;
                Product.Status = prod.TP_ProdInfo.Status;
                Product.AddDate = prod.TP_ProdInfo.AddDate;
                Product.Allowreply = prod.TP_ProdInfo.Allowreply;
                if (prod.TP_ProdOther != null)
                {
                    ProductProStr = TaoPortal.Common.Utility.JsonSerialization.SerializeObject(prod.TP_ProdOther.Select(x => new
                    {
                        ProID = x.ID,
                        ProText = x.ProText,
                        ProSort = x.Sort,
                        ProValue = x.ProValue
                    }).ToList());
                }
                if (prod.TP_ProdImg != null)
                {
                    ProductImgStr = TaoPortal.Common.Utility.JsonSerialization.SerializeObject(prod.TP_ProdImg.Select(x => new
                    {
                        FileID = x.FileID,
                        Sort = x.Sort,
                        Describe = x.Describe ?? "",
                        FileName = x.FileName ?? ""
                    }).ToList());
                }
            }
        }

        public void InitClass()
        {
            var classList = GetClassByType(TaoPortal.Common.SysEnum.SysClassType.ProductType);
            ProductClass = TaoPortal.Common.Utility.JsonSerialization.SerializeObject(classList);
            Product = new AddProduct
            {
                ClassID = (classList != null && classList.FirstOrDefault() != null) ? classList.FirstOrDefault().id : "",
                AddDate = DateTime.Now.Date
            };
        }
        public void InitID()
        {
            if (Product == null)
                Product = new AddProduct { ID = Guid.NewGuid().ToString() };
            else
                Product.ID = Guid.NewGuid().ToString();
        }

        public PagedList<ProdList> GetProductList()
        {
            var rp = new ProdRepository();
            var data = rp.GetProdInfoList(Searcher.PName, Searcher.PModel, null, Searcher.Status, Searcher.PClass, Searcher.OtherName, Searcher.OtherValue, Searcher.StartTime, Searcher.EndTime, PageIndex, PageSize);
            foreach (var item in data)
            {
                item.Status = HttpContextCache.GetParamValue(typeof(SysEnum.ArticleStatus).Name, item.Status);
            }
            return data;
        }

        public void Add()
        {
            var rp = new ProdRepository();
            TP_ProdInfo prod = new TP_ProdInfo
            {
                ID = Product.ID,
                AddDate = Product.AddDate,
                Allowreply = Product.Allowreply,
                ClassID = Product.ClassID,
                CreateBy = SessionEnt.UserName,
                CreateTime = DateTime.Now,
                Description = Product.Description,
                Introduction = Product.Introduction,
                PModel = Product.PModel,
                PName = Product.PName,
                PNumber = Product.PNumber,
                PPrice = Product.PPrice,
                Status = Product.Status,
                TitleImage = Product.TitleImage,
                UpdateBy = null,
                UpdateTime = null

            };
            List<TP_ProdImg> imgs = new List<TP_ProdImg>();
            if (Product.ProdImg != null)
            {
                foreach (var item in Product.ProdImg)
                {
                    imgs.Add(new TP_ProdImg
                    {
                        ID = Guid.NewGuid().ToString(),
                        ProdID = prod.ID,
                        FileID = item.FileID,
                        FileName = item.FileName,
                        Describe = item.Describe,
                        Sort = item.Sort
                    });
                }
            }
            List<TP_ProdOther> pros = new List<TP_ProdOther>();
            if (Product.ProductPro != null)
            {
                foreach (var item in Product.ProductPro)
                {
                    pros.Add(new TP_ProdOther
                    {
                        ID = Guid.NewGuid().ToString(),
                        IsBasePro = true,
                        ProdID = prod.ID,
                        ProText = item.ProText,
                        ProType = null,
                        ProValue = item.ProValue,
                        Sort = item.ProSort
                    });
                }
            }
            rp.AddProd(prod, imgs, pros);
        }

        public void Edit()
        {
            var rp = new ProdRepository();
            TP_ProdInfo prod = new TP_ProdInfo
            {
                ID = Product.ID,
                AddDate = Product.AddDate,
                Allowreply = Product.Allowreply,
                ClassID = Product.ClassID,
                Description = Product.Description,
                Introduction = Product.Introduction,
                PModel = Product.PModel,
                PName = Product.PName,
                PNumber = Product.PNumber,
                PPrice = Product.PPrice,
                Status = Product.Status,
                TitleImage = Product.TitleImage,
                UpdateBy = SessionEnt.UserName,
                UpdateTime = DateTime.Now

            };
            List<TP_ProdImg> imgs = new List<TP_ProdImg>();
            if (Product.ProdImg != null)
            {
                foreach (var item in Product.ProdImg)
                {
                    imgs.Add(new TP_ProdImg
                    {
                        ID = Guid.NewGuid().ToString(),
                        ProdID = prod.ID,
                        FileID = item.FileID,
                        FileName = item.FileName,
                        Describe = item.Describe,
                        Sort = item.Sort
                    });
                }
            }
            List<TP_ProdOther> pros = new List<TP_ProdOther>();
            if (Product.ProductPro != null)
            {
                foreach (var item in Product.ProductPro)
                {
                    pros.Add(new TP_ProdOther
                    {
                        ID = Guid.NewGuid().ToString(),
                        IsBasePro = true,
                        ProdID = prod.ID,
                        ProText = item.ProText,
                        ProType = null,
                        ProValue = item.ProValue,
                        Sort = item.ProSort
                    });
                }
            }
            rp.UpdateProd(prod, imgs, pros);
        }

        public void Delete(string id)
        {
            var rp = new ProdRepository();
            rp.DeleteProdByID(id);
        }

    }
    public class ProdClassModel : BaseModel
    {
        public string ID { get; set; }
        public string ClassType { get; set; }
        [Required]
        [Display(Name = "分类名称")]
        public string ClassName { get; set; }
        public string ParentID { get; set; }
        public string ParentClassName { get; set; }
        [Required]
        [Display(Name = "排序")]
        public Nullable<int> Sort { get; set; }
        public string Status { get; set; }
        public string Remark { get; set; }

        public string ProductClass { get; set; }

        public ProdClassModel()
        {
            ProductClass = "[]";
        }

        public void InitProdClass()
        {
            var classList = GetClassByType(TaoPortal.Common.SysEnum.SysClassType.ProductType);
            ProductClass = TaoPortal.Common.Utility.JsonSerialization.SerializeObject(classList);
        }

        public void Add()
        {
            var rp = new SystemRepository();
            rp.AddAppClass(new TP_AppClass { ID = Guid.NewGuid().ToString(), ParentID = ParentID, ClassName = ClassName, Sort = Sort, ClassType = ((int)TaoPortal.Common.SysEnum.SysClassType.ProductType).ToString() });
        }

        public void Edit()
        {
            var rp = new SystemRepository();
            rp.UpdateAppClass(new TP_AppClass
            {
                ID = this.ID,
                ParentID = this.ParentID,
                ClassName = this.ClassName,
                Sort = this.Sort
            });
        }

        public void Delete(string id)
        {
            var rp = new SystemRepository();
            rp.DeleteClass(id);
        }

        public void InitClass(string id)
        {
            var rp = new SystemRepository();
            var data = rp.GetClassByID(id);
            this.ID = data.ID;
            this.ClassName = data.ClassName;
            this.Sort = data.Sort;
            this.ParentID = data.ParentID;
        }

    }
}